package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.dto.PageDTO;
import com.woniu.dto.StockDto;
import com.woniu.exception.DIYException;
import com.woniu.model.HisStock;
import com.woniu.mapper.HisStockMapper;
import com.woniu.param.AddDrugsParam;
import com.woniu.param.DeleteParam;
import com.woniu.param.QueryPageParam;
import com.woniu.param.StockParam;
import com.woniu.service.IHisStockService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.util.CopyBeanUtil;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2021-11-12
 */
@Service
public class HisStockServiceImpl extends ServiceImpl<HisStockMapper, HisStock> implements IHisStockService {


    @Override
    public PageDTO<StockDto> getStockList(QueryPageParam queryPageParam) {
        Page<HisStock> page = new Page<>(queryPageParam.getPageParam().getPage(), queryPageParam.getPageParam().getPageSize());
        QueryWrapper<HisStock> qw = new QueryWrapper<>();
              if (null==queryPageParam){
                  throw  new DIYException(-9999,"分页参数为空");
              }else {
                  if (queryPageParam.getQueryStockParam()!=null){
                        if (queryPageParam.getQueryStockParam().getMedicineName()!=null){
                            qw.eq("medicine_name",queryPageParam.getQueryStockParam().getMedicineName());
                        }
                        if (queryPageParam.getQueryStockParam().getStockNotAvailable()!=null){
                            qw.eq("stock_not_available",queryPageParam.getQueryStockParam().getStockNotAvailable());
                        }
                  }
              }
        Page ipage = page(page, qw);
        List<StockDto> list = CopyBeanUtil.copyBean(ipage.getRecords(), StockDto.class);
        PageDTO<StockDto> bulid = PageDTO.bulid(page, list);

        return bulid;
    }

    @Override
    public StockDto updateStockList(StockParam stockParam) {
        HisStock hisStock = CopyBeanUtil.copyBean(stockParam, HisStock.class);
        boolean b = updateById(hisStock);
        if (!b){
            throw  new DIYException(-9999,"服务器异常");
        }
        return CopyBeanUtil.copyBean(stockParam,StockDto.class);
    }

    @Override
    public StockDto AddStrok(AddDrugsParam addDrugsParam) {
        HisStock hisStock = CopyBeanUtil.copyBean(addDrugsParam, HisStock.class);
          if (!save(hisStock)){
              throw  new DIYException(-9999,"服务器异常");
          }
        StockDto stockDto = CopyBeanUtil.copyBean(addDrugsParam, StockDto.class);
        return stockDto;
    }

    @Override
    public Boolean DelStok(DeleteParam deleteParam) {
        boolean b = removeByIds(deleteParam.getStockListIdParam());
        if (!b){
            throw  new DIYException(-9999,"服务器异常");
        }
        return b;
    }
}
